/*
 * Copyright (c) 2019.
 * http://www.ulabcare.com
 */

package com.mt.roll.core.utils;

import com.mt.roll.core.constants.RedisConstant;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 分布式锁帮助类<br>
 * 详细文档请查看<a href="https://github.com/redisson/redisson/wiki/8.-%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E5%92%8C%E5%90%8C%E6%AD%A5%E5%99%A8">分布式锁和同步器</a>
 * <p>
 * <pre class="code">
 * // 加锁以后10秒钟自动解锁
 * // 无需调用unlock方法手动解锁
 * lock.lock(10, TimeUnit.SECONDS);
 *
 * // 尝试加锁，最多等待100秒，上锁以后10秒自动解锁
 * boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
 * if (res) {
 *    try {
 *      ...
 *    } finally {
 *        lock.unlock();
 *    }
 * }
 * </pre>
 * </p>
 *
 * @author jiff
 * @date 2019-08-25
 * @since 1.0
 */
@Component
public class LockHelper {
    /**
     * 订单锁前缀：{@value}
     */
    private static final String ORDER_LOCK = RedisConstant.LOCK_PREFIX + "order:";
    private static final String FUND_LOCK = RedisConstant.LOCK_PREFIX + "fund:";
    private static final String SCHEDULER_LOCK = RedisConstant.LOCK_PREFIX + "scheduler:";
    /**
     * ip转地址任务调度锁：{@value}
     */
    private static final String IP_TO_ADDRESS_SCHEDULER_LOCK = SCHEDULER_LOCK + "ip_to_address";
    /**
     * uu跑腿定时提交订单任务锁
     */
    private static final String UUPT_SUBMIT_ORDER_SCHEDULER_LOCK = SCHEDULER_LOCK + "uupt_submit_order";

    private RedissonClient redissonClient;
    private static LockHelper lockHelper;

    @Autowired(required = false)
    public void setRedissonClient(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    @PostConstruct
    private void init() {
        lockHelper = this;
        lockHelper.redissonClient = this.redissonClient;
    }

    /**
     * 获取联锁
     *
     * @param locks 同时加锁的集合
     * @return
     */
    public static RLock getMultiLock(RLock... locks) {
        return lockHelper.redissonClient.getMultiLock(locks);
    }

    /**
     * 获取红锁
     *
     * @param locks 同时加锁的集合
     * @return
     */
    public static RLock getRedLock(RLock... locks) {
        return lockHelper.redissonClient.getRedLock(locks);
    }

    /**
     * 获取订单锁
     *
     * @param id 订单id
     * @return
     */
    public static RLock getOrderLock(Long id) {
        return lockHelper.redissonClient.getLock(ORDER_LOCK + id);
    }

    /**
     * 获取资金账户锁
     *
     * @param id 账户id
     * @return
     */
    public static RLock getFundLock(Long id) {
        return lockHelper.redissonClient.getLock(FUND_LOCK + id);
    }

    /**
     * 获取ip转地址任务调度锁
     *
     * @return
     */
    public static RLock getIpToAddressSchedulerLock() {
        return lockHelper.redissonClient.getLock(IP_TO_ADDRESS_SCHEDULER_LOCK);
    }

    /**
     * uu跑腿定时提交订单任务锁
     * @return
     */
    public static RLock getUuptSubmitOrderSchedulerLock(){
        return lockHelper.redissonClient.getLock(UUPT_SUBMIT_ORDER_SCHEDULER_LOCK);
    }


}
